package com.hellobike.haxing.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class SwaggerConfiguration {
    @Bean
    public Docket createRestApi() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("Access-Token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .globalOperationParameters(pars)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hellobike.haxing.web"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("哈行优选api ")
                .description("swagger RESTful APIs")
                .termsOfServiceUrl("http://www.test.com/")
                .version("1.0")
                .build();
    }
}
//@Component
//@Order(2)
//class MyApiModelPropertyPropertyBuilder implements ModelPropertyBuilderPlugin {
//    private final DescriptionResolver descriptions;
//
//
//    @Autowired
//    public MyApiModelPropertyPropertyBuilder(DescriptionResolver descriptions) {
//        this.descriptions = descriptions;
//    }
//
//    @Override
//    public void apply(ModelPropertyContext context) {
//        Optional<ApiModelProperty> annotation = Optional.absent();
//
//        if (context.getBeanPropertyDefinition().isPresent()) {
//            annotation = annotation.or(Annotations.findPropertyAnnotation((BeanPropertyDefinition) context.getBeanPropertyDefinition().get(), ApiModelProperty.class));
//        }
//
//        if (annotation.isPresent()) {
//            context.getBuilder().extensions(annotation.transform(toExtension()).orNull());
//        }
//    }
//
//    @Override
//    public boolean supports(DocumentationType delimiter) {
//        return SwaggerPluginSupport.pluginDoesApply(delimiter);
//    }
//
//    static Function<ApiModelProperty, List<VendorExtension>> toExtension() {
//        return new Function<ApiModelProperty, List<VendorExtension>>() {
//            public List<VendorExtension> apply(ApiModelProperty annotation) {
//                Extension[] extensions = annotation.extensions();
//                List<VendorExtension> list = new ArrayList<>();
//                if (extensions != null && extensions.length > 0) {
//                    for (int i = 0; i < extensions.length; i++) {
//
//                        Extension extension = extensions[i];
//                        String name = extension.name();
//                        ObjectVendorExtension objectVendorExtension = new ObjectVendorExtension(name);
//                        if (!name.equals("")) {
//                            for (int j = 0; j < extension.properties().length; j++) {
//                                ExtensionProperty extensionProperty = extension.properties()[j];
//                                StringVendorExtension stringVendorExtension = new StringVendorExtension(extensionProperty.name(), extensionProperty.value());
//                                objectVendorExtension.addProperty(stringVendorExtension);
//                            }
//                            list.add(objectVendorExtension);
//                        }
//                    }
//                }
//                return list;
//            }
//        };
//    }
//}
